<?php 
class MyZend_Controller_Action extends Zend_Controller_Action {
	
	public function init() {
		
	}
	
	/* LoadTemplate Function
	 * Load tempalte for all action of controller
	 * $template_path: direct of template
	 * $fileConfig: file config of template,default is template.ini
	 * $sectionConfig: section of file confif to use: default is [template]
	 */
	public function loadTemplate($template_path, $fileConfig = 'template.ini', $sectionConfig = 'template') {
		// delete all buffer front layout
		$this->view->headTitle()->set('');
		$this->view->headMeta()->getContainer()->exchangeArray(array());
		$this->view->headLink()->getContainer()->exchangeArray(array());
		$this->view->headScript()->getContainer()->exchangeArray(array());
		
		// read config in fileconfig
		$filename = $template_path . '/' . $fileConfig;
		$section  = $sectionConfig;
		$config   = new Zend_Config_Ini($filename, $section);
		// change data config to an array
		$config   = $config->toArray(); 
		
		// save data from config file 
		$baseUrl      = $this->_request->getBaseUrl();
		$templateUrl  = $baseUrl . $config['url'];
		$cssUrl	      = $templateUrl . $config['dirCss'];
		$jsUrl        = $templateUrl . $config['dirJs'];
		$imgUrl       = $templateUrl . $config['dirImg'];
		
		// load title
		$this->view->headTitle($config['title']);
		
		// load metaHttp
		if(count($config['metaHttp']) > 0) {
			foreach ($config['metaHttp'] as $key => $value) {
				$tmp = explode('|', $value);
				$this->view->headMeta()->appendName($tmp[0],$tmp[1]);
			}	
		}
		
		// load metaName
	    if(count($config['metaName']) > 0) {
			foreach ($config['metaName'] as $key => $value) {
				$tmp = explode('|', $value);
				$this->view->headMeta()->appendHttpEquiv($tmp[0],$tmp[1]);
			}	
		}
		
		// load css
		if(count($config['fileCss'])) {
			foreach ($config['fileCss'] as $key => $value) {
				$this->view->headLink()->appendStylesheet($cssUrl . $value,'screen');
			}
		}
		
		// load js
		if(count($config['fileJs']) > 0) {
			foreach ($config['fileJs'] as $key => $value) {
				$this->view->headScript()->appendFile($jsUrl . $value, 'text/javascript');
			}
		}
		
		// render all link url to view to use 
		$this->view->templateUrl = $templateUrl;
		$this->view->cssUrl      = $cssUrl;
		$this->view->jsUrl 	     = $jsUrl;
		$this->view->imgUrl		 = $imgUrl;
		
		// Load layout of template
		$option = array('layoutPath' => $template_path, 'layout' => $config['layout']);
		Zend_Layout::startMvc($option);
	}
}